package com.mjk.common.core.jdbc.service.log.model;


import com.mjk.common.base.model.BaseEntity;
import com.mjk.common.core.jdbc.annotation.*;
import com.mjk.common.core.jdbc.sphere.StrategyEnum;
import com.mjk.common.tools.gson.GsonUtils;
import com.mjk.common.tools.logger.BizLogger;
import com.mjk.common.util.ClobUtils;
import com.mjk.common.util.MDCUtils;
import com.mjk.common.util.ObjectUtils;
import io.seata.common.util.BlobUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.sql.Blob;
import java.sql.Clob;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * 全系统日志表 (记录API请求的错误数据，转化的错误数据，任务的错误数据)
 * 由于数据量问题，正常的数据不进行处理
 * @author Administrator
 *
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ArcSharding(table = "beelog", key = "createTime",strtegy = StrategyEnum.TIME )
public class BeeLogModel extends BaseEntity {
	
	public BeeLogModel(){}
	/**
	 * 
	 * @param itemId 可以关联的Id(可以包括接口记录ID,conver表id,task表id)
	 * @param itemType 项目类型 1为接口(apimode)2为转化任务(conver)3为任务(task)4为长SQL
	 * @param inParams 如果是1 为接口入参 2 转化参数 3 任务参数 4Sql语句
	 * @param outParams 如果是1 为接口出参 2 转化异常 3 任务异常  4 执行时间(>1000ms记录)
	 */
	public BeeLogModel(String itemId, String itemType, String inParams, String outParams) {
		this.tranceId= MDCUtils.getTraceId();
		this.itemId=itemId;
		this.itemType=itemType;
		this.inParams = ClobUtils.stringToClob(inParams);
		this.outParams = ClobUtils.stringToClob(outParams);
		this.createTime = LocalDateTime.now();
	}
	/**
	 * 此构造方法只试用在API
	 * @param work 请求的模块
	 * @param method 请求的方法
	 * @param itemType 类型为1 
	 * @param inParams 入参
	 * @param outParams 出参
	 */
	public BeeLogModel(String work, String method, String itemType, String inParams, String outParams) {
		this.tranceId=MDCUtils.getTraceId();
		this.workName = work;
		this.methodName = method;
		this.itemType = itemType;
		this.inParams = ClobUtils.stringToClob(inParams);
		this.outParams = ClobUtils.stringToClob(outParams);
		this.createTime = LocalDateTime.now();
		if(ObjectUtils.isNotEmpty(BizLogger.logStack.get())) {
			this.processContent = ClobUtils.stringToClob(GsonUtils.bean2Json(BizLogger.logStack.get()));
			BizLogger.logStack.remove();
		}
	}
	public BeeLogModel(String itemId, String work, String method, String itemType, String inParams, String outParams, String errorCode) {
		this.tranceId=MDCUtils.getTraceId();
		this.workName = work;
		this.methodName = method;
		this.itemType="1";
		this.inParams = ClobUtils.stringToClob(inParams);
		this.errorCode = errorCode;
		this.outParams = ClobUtils.stringToClob(outParams);
		this.createTime = LocalDateTime.now();
		if(ObjectUtils.isNotEmpty(BizLogger.logStack.get())) {
			this.processContent = ClobUtils.stringToClob(GsonUtils.bean2Json(BizLogger.logStack.get()));
			BizLogger.logStack.remove();
		}
	}
//	@Id
	@ExampleField
	@Filed()
	private String id;
	
	@ExampleField
	@Filed(comment = "tranceId")
	private String tranceId;
	
	@Filed(comment = "项目ID")
	private String itemId;
	
	@Filed(comment = "错误码")
	private String errorCode;
	
	@ExampleField
	@Filed(comment = "模块")
	private String workName;
	
	@ExampleField
	@Filed(comment = "方法")
	private String methodName;
	
	@ExampleField
	@Filed(comment = "项目类型 1Api 2 conver 3 task")
	private String itemType;
	
	@Filed(comment = "入参",lenght = 3000)
	private Clob inParams;
	
	@Filed(comment = "出参",lenght = 5000)
	private Clob outParams;

	//@Filed(comment = "异常数据",lenght = 3000)
	//private Clob exception;
	
	@Filed(comment = "调用过程日志",lenght = 5000)
	private Clob processContent;
	
	@ExampleField
	@Filed(comment = "创建时间")
	private LocalDateTime createTime;

	@Transient
	private String temp;

	@Transient
	private String time;

	@Transient
	private String count;

	public void addTemp(String logInfo) {
		if (ObjectUtils.isEmpty(temp)) {
			this.temp = logInfo;
		} else {
			this.temp = this.temp + "\r" + logInfo;
		}
	}
}